package com.v2future.sjt.collegecheckingattendance;

import android.app.Application;
import android.content.Context;

import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.PreferencesUtil;

import org.xutils.x;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

/**
 * base application
 * fun1：translate data insteal intent
 * fun2：cache data
 * fun3：save data
 * @author shiji_000
 *
 */
public class MainApplication extends Application {

    private static MainApplication mInstance = null;

    /**
     * hashMap for translating data
     */
    private HashMap<String, Object> intentData = null;

    /**
     * hashmap for caching data
     */
    private HashMap<String, Object> cache = null;

    public static MainApplication getInstance() {
        return mInstance;
    }

    public static Context getInstanceContext() {
        return mInstance.getApplicationContext();
    }

    /**
     * insert cache
     *
     * @param key
     * @param object
     * @Title: insertCathe
     * @Description: TODO
     * @author shiji_000
     */
    public void insertCathe(String key, Object object) {
        if (null != cache) {
            cache.put(key, object);
        } else {
            cache = new HashMap<String, Object>();
            cache.put(key, object);
        }
    }

    /**
     * get the data from cache
     *
     * @param key
     * @return
     * @Title: getIntentData
     * @Description: TODO
     * @author shiji_000
     */
    public Object getCathe(String key) {
        if (null != cache) {
            return cache.get(key);
        } else {
            cache = new HashMap<String, Object>();
            return null;
        }
    }

    /**
     * insert intent data
     *
     * @param key
     * @param object
     * @Title: insertIntentData
     * @Description: TODO
     * @author shiji_000
     */
    public void insertIntentData(String key, Object object) {
        if (null != intentData) {
            intentData.put(key, object);
        } else {
            intentData = new HashMap<String, Object>();
            intentData.put(key, object);
        }
    }

    /**
     * get intent data
     *
     * @param key
     * @return
     * @Title: getIntentData
     * @Description: TODO
     * @author shiji_000
     */
    public Object getIntentData(String key) {
        if (null != intentData) {
            return intentData.get(key);
        } else {
            intentData = new HashMap<String, Object>();
            return null;
        }
    }

    /**
     * clear intent data
     *
     * @Title: clearIntentData
     * @Description: TODO
     * @author shiji_000
     */
    public void clearIntentData() {
        if (null != intentData) {
            intentData.clear();
        }
    }

    /**
     * remove a cache
     *
     * @param key
     * @Title: removeCathe
     * @Description: TODO
     * @author shiji_000
     */
    public void removeCathe(String key) {
        if (null != cache) {
            cache.remove(key);
        }
    }

    /**
     * remove a intent
     *
     * @param key
     * @Title: removeIntent
     * @Description: TODO
     * @author shiji_000
     */
    public void removeIntent(String key) {
        if (null != intentData) {
            intentData.remove(key);
        }
    }

    /**
     * remove all cathe
     *
     * @Title: clearIntentData
     * @Description: TODO
     * @author shiji_000
     */
    public void clearCathe() {
        if (null != cache) {
            cache.clear();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        x.Ext.init(this);//xUtils3 init
        //init JPush
//        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        initPush();
    }


    public void setLoadingImage(int imageId) {

    }

    public void initPush(){
        if (PreferencesUtil.getSharedBooleanData(this, Constants.PREFERENCE_PUSH_ABLED)) {
            JPushInterface.resumePush(this);
        }
        else {
            JPushInterface.stopPush(this);
        }
    }
}
